import { Msg } from "../protobuf/msg";
import LogUtil from "../utils/LogUtil";
import { ProtoList } from "./Commond";
import Singleton from "./Singleton";

/**
 * proto数据解析
 */
export default class ParseData extends Singleton {
    private _root: any = Msg;

    /**
     * 测试
     */
    test() {
        let bund = Msg['RegR'].create();
        bund.username = 'aaa';
        bund.password= '111111';

        let arryUti = this.encode('RegR', bund);
        LogUtil.log('encode', arryUti);

        let arrayBuffer = new ArrayBuffer(4 + arryUti.length);
        let dataView = new DataView(arrayBuffer);
        dataView.setInt32(0,1001);
        for (let i = 0; i < arryUti.length; i++) {
            dataView.setUint8(i+4, arryUti[i]);
        }
        LogUtil.log('arrayBuffer>>>', arrayBuffer);

        let dataView1 = new DataView(arrayBuffer,0,4);
        let mid = dataView1.getInt32(0);
        LogUtil.log('mid>>>', mid);
        
        let buf = new Uint8Array(arrayBuffer,4);
        let deMessage = this.decode('RegR',buf);
        LogUtil.log('deMessage>>>', deMessage);
    }

    /**
     * 请求数据
     * @param req 
     * @param code 
     * @param data 
     * @returns 
     */
    reqData(req: string, code: number, data: any){
        let arryUti = this.encode(req, data);
        let arrayBuffer = new ArrayBuffer(4 + arryUti.length);
        let dataView = new DataView(arrayBuffer);
        dataView.setInt32(0,code);
        for (let i = 0; i < arryUti.length; i++) {
            dataView.setUint8(i+4, arryUti[i]);
        }
        return arrayBuffer;
    }

    /**
     * 返回数据
     * @param arrayBuffer 
     * @returns 
     */
    respData(arrayBuffer: any){
        // let bufView = new Uint8Array(arrayBuffer);
        let dataView = new DataView(arrayBuffer,0,4);
        let mid = dataView.getInt32(0);
        LogUtil.log('mid>>>', mid);
        let buf = new Uint8Array(arrayBuffer,4);
        let deMessage = null;
        switch(mid){
            case ProtoList.ChatMsg:
                deMessage = this.decode('ChatMsg',buf);
                break;
            default:
                LogUtil.warn('协议号未定义',mid);
                break
        }
        return deMessage;
    }


    /**
     * ArrayBuffer转为字符串
     * @param buf 
     * @returns 
     */
    ab2str(buf: any) {
        return String.fromCharCode.apply(null, new Uint8Array(buf));
    }

    /**
     * 字符串转为ArrayBuffer对象，参数为字符串
     * @param str 
     * @returns 
     */
    str2ab(str: string) {
        let buf = new ArrayBuffer(str.length); // 每个字符占用2个字节
        let bufView = new Uint8Array(buf);
        for (let i = 0, strLen = str.length; i < strLen; ++i) {
            bufView[i] = str.charCodeAt(i);
        }
        return buf;
    }

    /**
     * 序列化
     * @param  req 'BundleAssetR'
     * @param  data Msg['BundleAssetR'].create()
     * @returns 
     */
    encode(req: string, data: any) {
        let arryUti = this._root[req].encode(data).finish();
        return arryUti;
    }

    /**
     * 反序列化
     * @param  infoEncodeMessage 
     * @param  data
     * @returns 
     */
    decode(req: string, data: any) {
        let infoUnSerialized = this._root[req].decode(data);
        return infoUnSerialized;
    }

    /**
     * arrayBuffer转数组
     * @param buff 
     */
    arrayBufferToArray(arrayBuffer: ArrayBuffer): any[] {
        let array = Array.prototype.slice.call(new Uint8Array(arrayBuffer));
        return array;
    }

    /**
     * 数组转 arrayBuffer
     * @param array 
     * @returns 
     */
    arrayToArrayBuffer(array: any[]) {
        let arrayBuffer = new Uint8Array(array).buffer;
        return arrayBuffer;
    }
}